package GUI;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.KeyStroke;

import java.awt.Font;
import javax.swing.JButton;

import ControlLayer.ForestillingsCtr;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

public class OpretFilm {

	private JFrame frame;
	private JTextField tfTitel;
	private JTextField tfLaengde;
	private JTextField tfGenre;
	private JTextField tfPris;
	private JTextField tfBeskrivelse;
	private ForestillingsCtr filmCtr;
	private JCheckBox cb3D;

	/**
	 * Create the application.
	 */
	public OpretFilm() {
		filmCtr = new ForestillingsCtr();
		initialize();
		frame.setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame("Opret Film");
		frame.setBounds(100, 100, 302, 352);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		frame.setResizable(false);
		
		createLabels();
		textfields();	
	
		final JButton btnOpretFilm = new JButton("Opret Film");
		btnOpretFilm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(tfTitel.getText().equals("") || tfBeskrivelse.getText().equals("") || tfLaengde.getText().equals("") || tfGenre.getText().equals("") || tfPris.getText().equals(""))
				{
					JOptionPane.showMessageDialog(frame,
							"Error, fill in all textfields.", "Error Message",
							JOptionPane.ERROR_MESSAGE);
				}
				else
				{
					filmCtr.createFilm(tfTitel.getText(), tfBeskrivelse.getText(), tfLaengde.getText(), cb3D.isSelected(), tfGenre.getText(), Double.parseDouble(tfPris.getText()));
					frame.dispose();
				}
			}
		});
		btnOpretFilm.setBounds(178, 269, 97, 25);
		frame.getContentPane().add(btnOpretFilm);
		
		KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
		Action escapeAction = new AbstractAction() {
		    // close the frame when the user presses escape
		    public void actionPerformed(ActionEvent e) {
		    		      frame.dispose();
		    }
		}; 
		frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE");
		frame.getRootPane().getActionMap().put("ESCAPE", escapeAction);
		
		KeyStroke enterKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
		Action enterAction = new AbstractAction() {
		    // clicks the button when pressing enter
		    public void actionPerformed(ActionEvent e) {
		    	btnOpretFilm.doClick();
		    }
		}; 
		frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(enterKeyStroke, "ENTER");
		frame.getRootPane().getActionMap().put("ENTER", enterAction);
	}
	
	private void createLabels()
	{
		JLabel lblTitel = new JLabel("Titel:");
		lblTitel.setBounds(12, 70, 56, 16);
		frame.getContentPane().add(lblTitel);
		
		JLabel lblLngde = new JLabel("L\u00E6ngde:");
		lblLngde.setBounds(12, 99, 56, 16);
		frame.getContentPane().add(lblLngde);
		
		JLabel lblGenre = new JLabel("Genre: ");
		lblGenre.setBounds(12, 166, 56, 16);
		frame.getContentPane().add(lblGenre);
		
		cb3D = new JCheckBox("3D Film");
		cb3D.setBounds(129, 129, 113, 25);
		frame.getContentPane().add(cb3D);
		
		
		JLabel lblBilletPris = new JLabel("Billet Pris:");
		lblBilletPris.setBounds(12, 201, 82, 16);
		frame.getContentPane().add(lblBilletPris);
		
		JLabel lblFilmBeskrivelse = new JLabel("Film Beskrivelse:");
		lblFilmBeskrivelse.setBounds(12, 234, 116, 16);
		frame.getContentPane().add(lblFilmBeskrivelse);
		
		JLabel lblOpretFilm = new JLabel("Opret Film");
		lblOpretFilm.setFont(new Font("Tahoma", Font.PLAIN, 28));
		lblOpretFilm.setBounds(83, 20, 129, 34);
		frame.getContentPane().add(lblOpretFilm);
		
	}
	
	private void textfields()
	{
		tfTitel = new JTextField();
		tfTitel.setBounds(129, 67, 146, 22);
		frame.getContentPane().add(tfTitel);
		tfTitel.setColumns(10);
		
		tfLaengde = new JTextField();
		tfLaengde.setBounds(129, 96, 146, 22);
		frame.getContentPane().add(tfLaengde);
		tfLaengde.setColumns(10);
		
		tfGenre = new JTextField();
		tfGenre.setBounds(129, 163, 146, 22);
		frame.getContentPane().add(tfGenre);
		tfGenre.setColumns(10);
		
		tfPris = new JTextField();
		tfPris.setBounds(129, 198, 146, 22);
		frame.getContentPane().add(tfPris);
		tfPris.setColumns(10);
		
		tfBeskrivelse = new JTextField();
		tfBeskrivelse.setBounds(130, 230, 145, 25);
		frame.getContentPane().add(tfBeskrivelse);
		tfBeskrivelse.setColumns(10);
	}
}
